/*
 * SearchView.java
 *
 * Created on Apr 2, 2010, 8:42:50 PM
 */
package assetl.desktop;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultListModel;


import assetl.system.AssetLControl;
import assetl.system.DataPacket;
import assetl.system.Person;
import assetl.system.Asset;

/**
 * Allows an Admin to search through borrower history. A list
 * of past borrowers of a laptop can be shown or a list
 * of past laptops checked out to a person.
 * 
 * @author Devin Doman
 */
public class SearchView
   extends AssetView
{
   /**
    * The list model holding the past borrowers of an asset
    */
   private DefaultListModel mPersonListModel;
   /**
    * The list model holding the history of assets for a person
    */
   private DefaultListModel mAssetListModel;
   /**
    * Indicates whether the current function is searching for
    * past borrowers or whether it is searching for past history
    * of assets given to a person
    */
   private boolean mIsBorrowersSearch;

   /**
    * Constructor for the user interface
    *
    * @param pControl The controller for this view
    */
   public SearchView(AssetLControl pControl)
   {
      super(pControl);

      mPersonListModel = new DefaultListModel();
      mAssetListModel = new DefaultListModel();

      initComponents();

      mSearchBtn.addActionListener(new ActionListener()
      {
         /**
          * Updates data in the JList from the model
          * according to the current function set and
          * by the value in the text field
          */
         public void actionPerformed(ActionEvent ev)
         {
            updateData();
         }
      });
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      mSearchScroll = new javax.swing.JScrollPane();
      mItemList = new javax.swing.JList();
      mDataFld = new javax.swing.JTextField();
      mSearchBtn = new javax.swing.JButton();
      mItemsLbl = new javax.swing.JLabel();
      mDataLbl = new javax.swing.JLabel();
      mSearchLbl = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      mSearchScroll.setName("mSearchScroll"); // NOI18N

      mItemList.setName("mItemList"); // NOI18N
      mSearchScroll.setViewportView(mItemList);

      mDataFld.setName("mDataFld"); // NOI18N

      mSearchBtn.setText("Search");
      mSearchBtn.setName("mSearchBtn"); // NOI18N

      mItemsLbl.setText("Items:");
      mItemsLbl.setName("mItemsLbl"); // NOI18N

      mDataLbl.setText("Enter:");
      mDataLbl.setName("mDataLbl"); // NOI18N

      mSearchLbl.setText("Search");
      mSearchLbl.setName("mSearchLbl"); // NOI18N

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addComponent(mDataLbl)
                           .addComponent(mDataFld, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))
                     .addGroup(layout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addComponent(mSearchBtn)))
                  .addGap(69, 69, 69)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(mItemsLbl)
                     .addComponent(mSearchScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
               .addGroup(layout.createSequentialGroup()
                  .addGap(152, 152, 152)
                  .addComponent(mSearchLbl)))
            .addContainerGap(47, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addGap(22, 22, 22)
                  .addComponent(mSearchLbl)
                  .addGap(14, 14, 14)
                  .addComponent(mItemsLbl)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addComponent(mSearchScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addGroup(layout.createSequentialGroup()
                  .addGap(60, 60, 60)
                  .addComponent(mDataLbl)
                  .addGap(18, 18, 18)
                  .addComponent(mDataFld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(43, 43, 43)
                  .addComponent(mSearchBtn)))
            .addContainerGap(65, Short.MAX_VALUE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents
   // Variables declaration - do not modify//GEN-BEGIN:variables
   /**
   * Text field to enter search terms
   */
   private javax.swing.JTextField mDataFld;
   /**
   * Label for text field
   */
   private javax.swing.JLabel mDataLbl;
   /**
   * Contains the search results
   */
   private javax.swing.JList mItemList;
   /**
   * Label for JList
   */
   private javax.swing.JLabel mItemsLbl;
   /**
   * Button to Search with
   */
   private javax.swing.JButton mSearchBtn;
   /**
   * Search Label
   */
   private javax.swing.JLabel mSearchLbl;
   /**
   * Adds a Scroll pane to the JList
   */
   private javax.swing.JScrollPane mSearchScroll;
   // End of variables declaration//GEN-END:variables

   /**
    * Sets the mode the view is running in.
    *
    * @param pIsBorrowersSearch True if a borrow search is to be performed
    */
   public void setMode(boolean pIsBorrowersSearch)
   {
      mIsBorrowersSearch = pIsBorrowersSearch;

      if (mIsBorrowersSearch)
      {
         setLabeling("Search Past Borrowers", "Enter Laptop barcode:",
            "Borrowers:");
         mItemList.setModel(mPersonListModel);
      }
      else
      {
         setLabeling("Search Laptops Borrowed", "Enter I-number:",
            "Laptops borrowed:");
         mItemList.setModel(mAssetListModel);
      }
   }

   /**
    * Sets the view labeling
    *
    * @param pHeader The header label value
    * @param pPrompt The prompt for data to be entered in the text field
    * @param pList The list description
    */
   public void setLabeling(String pHeader, String pPrompt, String pList)
   {
      mSearchLbl.setText(pHeader);
      mDataLbl.setText(pPrompt);
      mItemsLbl.setText(pList);
   }

   /**
    * Updates the views display of the model
    */
   public void updateData()
   {
      if (mIsBorrowersSearch)
      {
         updateBorrowers();
      }
      else
      {
         updateAssetHistory();
      }
   }

   /**
    * Updates the JList with a list of past borrowers of an asset
    */
   public void updateBorrowers()
   {
      Asset asset = new Asset();
      asset.setID(mDataFld.getText());
      if (asset != null)
      {
         for (Person person : mControl.getPastBorrowers(asset))
         {
            mPersonListModel.addElement(person);
         }
      }
   }

   /**
    * Updates the JList with a history of assets borrowed by a person
    */
   public void updateAssetHistory()
   {
      Person person = ModelObjectGenerator.fetchPerson(mDataFld.getText());
      if (person != null)
      {
         for (Asset asset : mControl.getAssetHistory(person))
         {
            mAssetListModel.addElement(asset);
         }
      }
   }

   /**
    * Grabs all pertainent data from the fields and sets them in
    * a DataPacket object. This DataPacket is then returned by the
    * method.
    *
    * @param pFunction The function that needs the DataPacket
    * @return The DataPacket that has been set
    */
   public DataPacket grabDataPacket(String pFunction)
   {
      //No packet to send
      return null;
   }

   /**
    * Enables functionality passed in for this view.
    */
   public void enable(String pFunction)
   {
      setMode("SearchBorrowers".equals(pFunction));
   }

   /**
    * The entry point for this view
    */
   public void run()
   {
   }
}
